package com.epsoft.odin7cj.flow.api.flowtmpl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.output.ByteArrayOutputStream;

//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.repository.Deployment;

import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.Deployment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.epsoft.odin7cj.core.dto.RetData;
import com.epsoft.odin7cj.core.exception.BaseException;
import com.epsoft.odin7cj.core.model.PageBean;
import com.epsoft.odin7cj.core.util.JhkjUUIDUtil;
import com.epsoft.odin7cj.flow.dao.flowtmpl.FlowTmplDao;
import com.epsoft.odin7cj.flow.enums.FlowRetCodeEnum;
import com.epsoft.odin7cj.flow.model.deploy.FlowDeployment;
import com.epsoft.odin7cj.flow.model.flowtmpl.FlowTmpl;
import com.epsoft.odin7cj.flow.util.BpmnBeanUtils;
import com.epsoft.odin7cj.flow.util.BpmnPicUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * DESC: 流程模板 <br>
 * @author 胡义振
 * @date 2020年5月20日
 */
@RestController
@RequestMapping("/api/flow/tmpl")
public class FlowTmplApi {

	@Autowired
	private FlowTmplDao flowTmplDao;
	@Autowired
	private RepositoryService repositoryService;
	
    /**
     * Desc: 查询所有流程发布配置表
     * @param auditDeployment  
     * @return
     * @throws Exception
     */
	@RequestMapping("/getFlowTmplList")
    public RetData<PageInfo<FlowTmpl>> doGetFlowTmplList(FlowTmpl flowTmpl,PageBean pageBean) throws Exception {
		RetData<PageInfo<FlowTmpl>> retData = new RetData<PageInfo<FlowTmpl>>();
		PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
		List<FlowTmpl> listDeployLib = flowTmplDao.selectList(flowTmpl);
		PageInfo<FlowTmpl> pageInfo = new PageInfo<FlowTmpl>(listDeployLib);
		retData.setRetBody(pageInfo);
    	return retData;
    }
	
	@RequestMapping("/getFlowTmpl")
    public RetData<FlowTmpl> doGetFlowTmpl(String id) throws Exception {
		RetData<FlowTmpl> retData = new RetData<FlowTmpl>();
		FlowTmpl flowTmpl = flowTmplDao.selectById(id);
    	retData.setRetBody(flowTmpl);
    	return retData;
    }
	
	@RequestMapping("/addFlowTmpl")
    public RetData<String> doSaveFlowTmpl(FlowTmpl flowTmpl) throws Exception {
		RetData<String> retData = new RetData<String>();
		flowTmpl.setId(JhkjUUIDUtil.getUUID());
		flowTmplDao.insertOne(flowTmpl);
    	retData.setRetBody("");
    	return retData;
    }
	
	@RequestMapping("/editFlowTmpl")
    public RetData<String> doEditFlowTmpl(FlowTmpl flowTmpl) throws Exception {
		RetData<String> retData = new RetData<String>();
		flowTmplDao.updateById(flowTmpl);
    	retData.setRetBody("");
    	return retData;
    }
	
	@RequestMapping("/delFlowTmpl")
    public RetData<String> doDelFlowTmpl(String tmplId) throws Exception {
		RetData<String> retData = new RetData<String>();
		flowTmplDao.deleteById(tmplId);
    	retData.setRetBody("");
    	return retData;
    }
	
	/**
	 * DESC: 流程发布
	 * @param deployName 流程的名称 
	 * @param bpmnPath 资源文件的 bpmn 目录中的资源（不含后缀）
	 * @return
	 */
	@RequestMapping("/deploy")
	public RetData<FlowDeployment> deployBpmn(String tmplId)  throws Exception {
		RetData<FlowDeployment> retData = new RetData<FlowDeployment>();
		
		FlowTmpl flowTmpl = flowTmplDao.selectById(tmplId);
		if(flowTmpl==null) {
			throw new BaseException(FlowRetCodeEnum.RET_CODE_0501001.getCode(),FlowRetCodeEnum.RET_CODE_0501001.getMessage());
		}
		if(flowTmpl.getBpmnXml()==null || flowTmpl.getBpmnXml().equals("")) {
			throw new BaseException(FlowRetCodeEnum.RET_CODE_0501002.getCode(),FlowRetCodeEnum.RET_CODE_0501002.getMessage());
		}
		Deployment deployment = null;
		try
		{
			InputStream bpmnInputStream = new ByteArrayInputStream(flowTmpl.getBpmnXml().getBytes("utf-8"));
			ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
			BpmnPicUtils.svgConvertToPng(flowTmpl.getBpmnSvg(), outPutStream);
			InputStream pngInputStream = new ByteArrayInputStream(outPutStream.toByteArray());
			deployment = repositoryService.createDeployment()
					.name(flowTmpl.getTmplName())
					.addInputStream(flowTmpl.getTmplKey()+".bpmn", bpmnInputStream)
					.addInputStream(flowTmpl.getTmplKey()+".png", pngInputStream)
					
					.addClasspathResource("form/start.html")
					.addClasspathResource("form/fill.html")
					.addClasspathResource("form/audit_dept.html")
					.addClasspathResource("form/audit_fina.html")
					
					.deploy();
		}
		catch(Exception er) {
			System.out.println("\n 流程发布出现异常：");
			er.printStackTrace();
		}
		retData.setRetBody(BpmnBeanUtils.convertToFlowDeployment(deployment));
		return retData;
	}
	
}
